package com.hlf.robot.listener;


import cn.hutool.core.io.resource.ClassPathResource;
import com.hlf.robot.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import love.forte.simboot.annotation.ContentTrim;
import love.forte.simboot.annotation.Filter;
import love.forte.simboot.annotation.Listener;
import love.forte.simbot.Identifies;
import love.forte.simbot.event.GroupMessageEvent;
import love.forte.simbot.message.At;
import love.forte.simbot.message.Face;
import love.forte.simbot.message.MessagesBuilder;
import love.forte.simbot.resources.Resource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;

/**
 * @Author: 郝龙飞
 * @Date: 2024/01/19/16:17
 * @Description: 群聊监听器
 */
@Component
@Slf4j
public class GroupListener {

    @Listener
    @Filter(targets = @Filter.Targets(atBot = true))
    @ContentTrim // 当匹配被at时，将'at'这个特殊消息移除后，剩余的文本消息大概率存在前后空格，通过此注解在匹配的时候忽略前后空格
    public CompletableFuture<?> onChannelMessage(GroupMessageEvent event) throws IOException { // 将要监听的事件类型放在参数里，即代表监听此类型的消息
        log.info("进入at通用监听器，开始处理消息");
        //备注 getBot是机器人（即登录号） getSource（是来源群）getOwnerId是获取群主id
        At at = new At(event.getSource().getOwnerId());
        String plainText = event.getMessageContent().getPlainText();
        log.info("获取到的消息文本================plainText==================");
        //从群组被at获取到的消息文本
        log.info("监听到的plainText:{}", plainText);
        //对消息进行处理
        if (plainText.trim().equals(Constants.actionList.MARRY)){
            //结婚
            marry(event);
        } else if (plainText.trim().equals(Constants.actionList.SIGN_IN)) {
            signIn(event);
        }
        log.info("at通用监听器事件处理结束");
        return null;
    }

    private void signIn(GroupMessageEvent event) throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("image/1.jpg");
        MessagesBuilder builder = new MessagesBuilder();
        builder.append("月下饮茶，念卿天涯。");
        builder.append(new Face(Identifies.ID(144)));
        builder.image(Resource.of(classPathResource.getStream()));
        event.replyBlocking(builder.build());
    }

    private void marry(GroupMessageEvent event) {
        event.getSource().sendBlocking("结婚成功");
    }


}
